package com.nearsoft.academy.sharounding;

import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.commons.collections15.CollectionUtils;
import org.apache.commons.collections15.Predicate;
import org.jumpmind.symmetric.csv.CsvReader;

/**
 * Hello world!
 */
public class DataAnalyticsReader {

    private List<PathTest> testPathList;
    private int errors;

    public List<PathTest> getPathTestList() {
        return this.testPathList;
    }

    public DataAnalyticsReader() {
        this.testPathList = new ArrayList<PathTest>();
    }

    public void readOrientationDataList(String fileName) throws IOException, ParseException {
        CsvReader reader = new CsvReader(getClass().getResource(fileName).getFile(), ',');
        PathTest currentTest = null;

        while (reader.readRecord()) {
            String[] values = reader.getValues();
            String testName = values[0];
            if (currentTest == null || !currentTest.getName().equals(testName)) {
                currentTest = findTest(this.testPathList, testName);
                if (currentTest == null) {
                    currentTest = new PathTest(testName);
                    this.testPathList.add(currentTest);
                }
            }

            Date sampleDate = parseDate(values[1]);
            float headingAngle = Float.parseFloat(values[2]);
            float pitchAngle = Float.parseFloat(values[3]);
            float rollAngle = Float.parseFloat(values[4]);
            OrientationData data = new OrientationData(sampleDate, headingAngle, pitchAngle, rollAngle);
            currentTest.addOrientationData(data);
        }

        reader.close();
    }

    public static PathTest findTest(List<PathTest> testList, final String testName) {
        return CollectionUtils.find(testList, new Predicate<PathTest>() {
            public boolean evaluate(PathTest pathTest) {
                return testName.equals(pathTest.getName());
            }
        });
    }

    private Date parseDate(String value) throws ParseException {

        long millisecs = Long.parseLong(value);

        return new Date(millisecs);
    }

    public List<OrientationData> readOrientationDataList() {

        List<OrientationData> orientationList = new ArrayList<OrientationData>();

        return orientationList;
    }

    public void readAll(String filenameOrientation) throws IOException, ParseException {
        this.readOrientationDataList(filenameOrientation);
    }

    public String toString() {
        List<PathTest> testList = this.getPathTestList();

        StringBuilder result = new StringBuilder();

        result.append("Total tests: ");
        result.append(testList.size());
        result.append("\n");

        result.append("Rotation speed: ");
        result.append("\n");

        result.append("Avrg: ");
        result.append(PathTest.getAvrgRotationSpeed(this.testPathList));
        result.append("\n");

        result.append("Max: ");
        result.append(PathTest.getMaxRotationSpeed(this.testPathList));
        result.append("\n");

        result.append("Min: ");
        result.append(PathTest.getMinRotationSpeed(this.testPathList));
        result.append("\n\n-----\n");
        result.append("Test Name, Duration, Errors Rate, Average Speed");
        result.append("\n");
        for (PathTest test : testList) {
            result.append(test.getName());
            result.append(", ");
            result.append(test.getDurationInSecondsString());
            result.append(", ");
            result.append(test.getErrorsRate());
            result.append(", ");
            result.append(test.getAvrgRotationSpeed());
            result.append("\n");
        }

        return result.toString();
    }


}
